package org.demo.five;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;

/**
 * 
 * @author  
 * @date    2010-10-25
 * @file    org.demo.five.ABQApp.java
 */
public class ABQApp {

	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws InterruptedException {
		BlockingQueue<String> drop = new SynchronousQueue<String>();
		new Thread(new Producer(drop)).start();		
		new Thread(new Consumer(drop)).start();
	}

}
class Producer implements Runnable{
	private BlockingQueue<String> drop;
	List<String> messages = Arrays.asList(
			               "Mares eat oats",
			               "Does eat oats",
			               "Little lambs eat ivy",
			               "wouldn't you eat ivy too?");
	
	public Producer(BlockingQueue<String> d){drop = d;}
	
	@Override
	public void run() {
		try {
			for (String msg : messages){
				drop.put(msg);
			}
			drop.put("DONE");
		} catch (Exception e) {
			System.out.println("Interrupted! Last one out,turn out the lights.");
		}
	}
}
class Consumer implements Runnable{
	private BlockingQueue<String> drop;
	public Consumer(BlockingQueue<String> d){drop = d;}
	
	@Override
	public void run() {
		String msg = null;
		try {
			while (true){
				msg = drop.take();
				if (!msg.equals("DONE")){
					System.out.println(msg);
				} else {
					break;
				}
			}
		} catch (InterruptedException e) {
			System.out.println("Interrupted! Last one out, turn out the lights.");
		}
	}
}